import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
const baseUrl = '/finance/';
const path = require('path');
export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1611106403330_5590';

  // add your egg config in here
  config.middleware = [ 'authorization', 'errorHandler' ];
  // errorHandler 只在/api上生效
  config.errorHandler = {
    match: '/finance',
  };
  config.authorization = {
    enable: true,
    ignore: `${baseUrl}check/login`, // 只忽略指定路由，可以用ignore
  };
  config.mysql = {
    client: {
      // host
      host: 'localhost',
      // 端口号
      port: '3306',
      // 用户名
      user: 'root',
      // 密码
      password: '123456',
      // 数据库名
      database: 'finance',
    },
    // 是否加载到 app 上，默认开启
    app: true,
    // 是否加载到 agent 上，默认关闭
    agent: false,
  };
  // 配置跨域
  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true,
    },
    domainWhiteList: [ 'http://localhost:8080', 'http://127.0.0.1:8080' ],
  };
  config.logger = {
    dir: './logs/local', // 打印目录重定向
    outputJSON: true, // json格式输出
  };

  config.sequelize = {
    dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
    database: 'finance', // 数据库名
    host: '127.0.0.1',
    port: 3306,
    username: 'root', // 账号
    password: '123456', // 密码
    timezone: '+08:00', // 保存为本地时区,mysql保存时会自动保存为UTC格式
    define: {
      timestamps: false,
    },
    dialectOptions: {
      dateStrings: true,
      typeCast(field, next) {
        if (field.type === 'DATETIME') {
          return field.string();
        }
        return next();
      },
    },
  };
  // token加密秘钥
  config.jwt = {
    secret: 'forajlsjuiuioouee9999', // 自定义 token 的加密条件字符串
  };
  config.cors = {
    origin: '*', // 匹配规则  域名+端口  *则为全匹配
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
  };
  config.keys = appInfo.name + '_1576461360545_5788';
  config.valparams = {
    locale: 'zh-cn',
    throwError: false,
  };
  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };
  config.cryptoConfig = {
    secret: 'Z#fOGf$te4^J29l1Z&$#fXCNifv!ZHQnEG',
  };
  // 读取文件配置
  config.multipart = {
    mode: 'file',
    fileSize: '10mb',
  };
  config.static = {
    // 静态化访问前缀,如：`http://127.0.0.1:7001/static/images/logo.png`
    prefix: '/',
    dir: path.join(appInfo.baseDir, 'app/public'), // `String` or `Array:[dir1, dir2, ...]` 静态化目录,可以设置多个静态化目录
    dynamic: true, // 如果当前访问的静态资源没有缓存，则缓存静态文件，和`preload`配合使用；
    preload: false,
    maxAge: 31536000, // in prod env, 0 in other envs
    buffer: true, // in prod env, false in other envs
  };
  // the return config will combines to EggAppConfig
  return {
    ...config as {},
    ...bizConfig,
  };
};
